import 'package:casdon/common/service/at_auth_service.dart';
import 'package:casdon/common/service/base_service.dart';
import 'package:casdon/common/service/tuya_service.dart';
import 'package:casdon/common/service/wechat_service.dart';

class ServiceManager {
  static final ServiceManager _instance = ServiceManager._internal();
  factory ServiceManager() => _instance;
  ServiceManager._internal();

  final Map<Type, BaseService> _services = {};

  Future<void> initServices() async {
    // 注册所有服务
    _registerService(TuyaService());
    _registerService(AtAuthService());
    _registerService(WechatService());
    
    // 初始化所有服务
    for (final service in _services.values) {
      await service.init();
    }
  }

  T getService<T extends BaseService>() {
    final service = _services[T];
    if (service == null) {
      throw Exception('Service $T not registered');
    }
    return service as T;
  }

  Future<void> dispose() async {
    for (final service in _services.values) {
      await service.dispose();
    }
    _services.clear();
  }

  void _registerService<T extends BaseService>(T service) {
    _services[T] = service;
  }
}